package com.ssu.judea.travlog.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;

import statics.TravlogVars;

import com.ssu.judea.test.TestDAO;
import com.ssu.judea.test.TestServlet;
import com.ssu.judea.test.TestVO;
import com.ssu.judea.travlog.model.WebDAO;
import com.ssu.judea.travlog.vo.DatePost;
import com.ssu.judea.travlog.vo.Member;

import comm.util.CommUtil;
import comm.util.RequestManager;
import comm.util.TravlogHashMap;

public class WebServlet extends TravlogServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Logger logger = Logger.getLogger(WebServlet.class);
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		super.service(request,response);
		TravlogHashMap hm = RequestManager.getRequest(request);
		
		WebDAO dao = new WebDAO();
		logger.debug("=============WebServlet getServletPath()=======");
		logger.debug(request.getServletPath());
		logger.debug("=============WebServlet getRequestURI()=======");
		logger.debug(request.getRequestURI());
		
		if(request.getServletPath().equals(TravlogVars.SERVLET_PATH_USER)){
			// 루트디렉토리 밑에 /user 서블릿
			String requestUri = request.getRequestURI().substring(request.getRequestURI().indexOf(TravlogVars.SERVLET_PATH_USER)+TravlogVars.SERVLET_PATH_USER.length());
			String parameters[] = requestUri.split("/");
			if(parameters.length<2){
				//파라미터 없을시에는 user home으로~
				ArrayList<DatePost> list = dao.getUserPostDateListArrayList(request.getSession().getAttribute("SESSION_MEMBER_SEQ").toString());
				request.setAttribute("LIST", list);
				request.getRequestDispatcher("/jsp/web/userhome.jsp").forward(request, response);
			}else{
				//파라미터 있을시 구분 ㄱㄱㄱ
				if(parameters.length==2){
					//파라미터 하나(memberid)
					String memberid = parameters[1];
					Member member = dao.getMemberById(memberid);
					request.setAttribute("Member", member);
					request.setAttribute(TravlogVars.ATTR_USER_ID, memberid);
					request.getRequestDispatcher("/jsp/web/userhome.jsp").forward(request, response);
				}else if(parameters.length==3 && 
						(TravlogVars.PARAMS_USER_SUB_CALENDAR.equals(parameters[2])||
								TravlogVars.PARAMS_USER_SUB_MYLIST.equals(parameters[2])||
								TravlogVars.PARAMS_USER_SUB_FOLLOWERS.equals(parameters[2])
						)){
					//파라미터 둘 (memberid/sub) sub:calendar,mylist,followers
					String memberid = parameters[1];
					String sub = parameters[2];
					Member member = dao.getMemberById(memberid);
					request.setAttribute("Member", member);
					request.setAttribute(TravlogVars.ATTR_USER_ID, memberid);
					request.setAttribute(TravlogVars.ATTR_USER_SUB, sub);
					if(sub.equals(TravlogVars.PARAMS_USER_SUB_MYLIST)){
						ArrayList<DatePost> list = dao.getUserPostDateListArrayList(String.valueOf(member.getMemberseq()));
						request.setAttribute("LIST", list);
						
					}else if(sub.equals(TravlogVars.PARAMS_USER_SUB_FOLLOWERS)){
						ArrayList<DatePost> list = dao.getFollowersList(String.valueOf(member.getMemberseq()));
						request.setAttribute("LIST", list);
					}
					request.getRequestDispatcher("/jsp/web/userhome.jsp").forward(request, response);
				}else{
					response.sendRedirect("http://"+request.getServerName()+request.getContextPath()+request.getServletPath());
				}
			}
			
			
		}else if(request.getServletPath().lastIndexOf(".trav")>0){
			//.trav Action이 있는 서블릿
			if(hm==null || hm.get("ACTION")==null || hm.get("ACTION").toString().length()<1){
				request.getRequestDispatcher("/jsp/web/main.jsp").forward(request, response);
			}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("toauthpage")){			
				dao.toAuthpage(request, response);
			}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("logout")){
				request.getSession().invalidate();
				request.setAttribute("dispatchAction", "afterLogout");
				request.getRequestDispatcher("/jsp/web/main.jsp").forward(request, response);
			}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("callback")){
				try {
					Member result = dao.authorize(request,response);
					if(result.getMemberseq()>0){
						request.setAttribute("AUTH_RESULT", TravlogVars.SUCCESS);
						request.setAttribute("MEMBER", result);
						
						request.getSession().setAttribute("SESSION_MEMBER_SEQ", result.getMemberseq());
						request.getSession().setAttribute("SESSION_MEMBER_TWITTER_ID", result.getNick());
						request.getSession().setAttribute("SESSION_MEMBER_IMAGE_URL", result.getProfileurl());
					}else{
						request.setAttribute("AUTH_RESULT", TravlogVars.FAIL);
					}
					
					request.getRequestDispatcher("/jsp/web/commons/callback.jsp").forward(request, response);
				} catch (ParserConfigurationException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
}
